// 获取应用实例
import Toast from '@vant/weapp/toast/toast';
const app = getApp()

Page({
  data: {
    height: '',
    weight: '',
    BMI: '',
    condition: ''
  },
  onLoad() {
    this.setData({
      height: '',
      weight: '',
      BMI: '',
      condition: ''
    })
  },
  getHeight: function (e) {
    this.setData({
      height: e.detail
    })
  },
  getWeight: function (e) {
    this.setData({
      weight: e.detail
    })
  },
  handleWarning() {
    Toast.fail('请输入身高体重');
  },
  calculate: function () {
    let height = this.data.height / 100;
    let weight = this.data.weight;
    if (height == '' || weight == '') {
      this.handleWarning();
      return;
    }
    let BMI = weight / (height * height);
    var condition;
    if (BMI < 18.5) {
      condition = '偏瘦';
    } else if (BMI < 24) {
      condition = '正常';
    } else if (BMI < 28) {
      condition = '偏胖';
    } else if (BMI < 32) {
      condition = '肥胖';
    } else {
      condition = '严重肥胖';
    }
    this.setData({
      BMI: BMI.toFixed(2), //保留两位小数
      condition: condition
    })
  },
  retest: function () {
    this.setData({
      height: '',
      weight: '',
      BMI: '',
      condition: ''
    })
  }
})